<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use think\facade\Session;
use app\model\SettingUser as UserModel;
use app\model\SettingUsergroup as UsergroupModel;
use app\model\LogLogin as LoginLogModel;

class Login
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {   
        //渲染首页
        return view('index');
    }

    /**
     * 验证登陆信息.
     *
     * @return \think\Request
     */
    public function logincheck(Request $request)
    {
        //判断模式
        $type = $request->param('type');
        
        if($type == 'query'){
            $where['phone'] = '库存查询';
            $where['password'] = md5((string)trim($request->param('queryPassword')));
        }elseif ($type == 'login') {
            $where['phone'] = trim($request->param('phone'));
            $where['password'] = md5((string)trim($request->param('password')));
        }else{
            //写入访问日志
            $log['name'] = trim($request->param('phone'));
            $log['ip'] = $request->ip();
            $log['time'] = date('Y-m-d H:i:s',$request->time());
            $log['state'] = 0;
            $log['note'] = '未指定登陆方式';
            LoginLogModel::create($log);

            $result['user'] = false;
            $result['session'] = false;
            
            return json($result);
            exit();
        }

        $userData = UserModel::where($where)->find();

        if(!empty($userData)){
            $result['user'] = true;
            // 设置session
            Session::set('username', $userData['name']);
            Session::set('userId', $userData['id']);
            Session::set('usergroup',$userData['userAuthLevel']);

            $userGroup = UsergroupModel::find($userData['userAuthLevel']);
            Session::set('nav_class', $userGroup['nav_class']);
            Session::set('nav_detail', $userGroup['nav_detail']);

            if(Session::has('username')){
                //写入访问日志
                $log['name'] = $userData['name'];
                $log['ip'] = $request->ip();
                $log['time'] = date('Y-m-d H:i:s',$request->time());
                $log['state'] = 1;
                $log['note'] = '登陆成功';
                LoginLogModel::create($log);

                $result['session'] = true;
                if(Session::has('lastLink')){
                    $result['link'] = Session::get('lastLink');
                }else{
                    if($type == 'query'){
                        $result['link'] = (string)url('Stock/queryStock');
                    }elseif ($type == 'login'){
                        $result['link'] = (string)url('Showroom/index');
                    }
                }
                return json($result);
            }else{
                //写入访问日志
                $log['name'] = $userData['name'];
                $log['ip'] = $request->ip();
                $log['time'] = date('Y-m-d H:i:s',$request->time());
                $log['state'] = 0;
                $log['note'] = '设置session失败';
                LoginLogModel::create($log);

                $result['session'] = false;
                return json($result);
            }
        }else{
            //写入访问日志
            $log['name'] = $request->param('phone');
            $log['ip'] = $request->ip();
            $log['time'] = date('Y-m-d H:i:s',$request->time());
            $log['state'] = 0;
            $log['note'] = '用户名及密码错误 用户名：'. trim($request->param('phone')).'，密码：'.md5((string)trim($request->param('password')));
            LoginLogModel::create($log);

            $result['user'] = false;
            $result['session'] = false;
            return json($result);
        }
    }

    /**
     * 退出登陆信息.
     *
     * @return \think\Request
     */
    public function logon(Request $request)
    {
        //
        Session::clear();
        if(!Session::has('username')){
            return redirect((string)url('login/index'));
        }else{
            return redirect((string)url('index/index'));
        }
    }

}
